"""
@Project : pythonExample
@File    : 类与对象.py
@IDE     : PyCharm
@Author  : RR9
@Date    : 2024/08/26 14:54
@Explain :
"""

from loguru import logger

"""类及对象
类名使用驼峰命名法（CameClass）,即每个单词的字母都是大写，且不是用下划线
类实例表示形式：class_name.__repr__
类实例打印形式：class_name.__str__
"""


class Pair:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f'__repr__:({self.x}, {self.y})'

    def __str__(self):
        return f'__str__:({self.x}, {self.y})'


class Pair2:
    def __init__(self, x, y):
        self.x = x
        self.y = y


_formats = {
    'ymd': '{d.year}-{d.month}-{d.day}',
    'mdy': '{d.month}/{d.day}/{d.year}',
    'dmy': '{d.day}/{d.month}/{d.year}'
}


class DateFormat:
    def __init__(self, year: str, month: str, day: str):
        self.year = year
        self.month = month
        self.day = day

    def __format__(self, code):
        if code == '':
            code = 'ymd'
        fmt = _formats[code]
        return fmt.format(d=self)


class Person:
    def __init__(self, name):
        self.name = name

    @property
    def name(self) -> str:
        return self._name

    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected a string')
        print('name的setter被触发了')
        self._name = value

    @name.deleter
    def name(self):
        raise AttributeError("Can't delete attribute")
